#include <stdafx.h>
#include <XButton.h>


XPushButton::XPushButton(XHash hash, XWindow* window)
	: XWidget(hash, window)
{
	Down = false;

	Render = new XRenderPack(Hash.String + "-Layout");
	Text = new XTextPack(Hash);
	Text->SetFont(ParentWindow->GetFont());

	window->RegisterControlsRender(Render);
	window->RegisterControlsText(Text);

	_loadVertices();
	_loadTextureVertices();
}

void XPushButton::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	_loadVertices();

	int space = 0;
	int height = 0;

	if(Text != NULL)
	{
		space = Rectangle.W - Text->GetWidth();
		height = Rectangle.H - Text->GetHeight();
	}

	space /= 2;
	height /= 2;

	if(Text != NULL)
		Text->SetTextBox(XRectangle(Rectangle.X + space, Rectangle.Y + height, Rectangle.W, Rectangle.H));
}

void XPushButton::_loadTextureVertices()
{


	XVertex* vert = new XVertex[4];
	if(Down == false)
	{
		vert[0].X = (float)512/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)4/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)664/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)4/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)512/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)36/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)664/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)36/ParentWindow->GetTexture()->GetHeight();
	}
	else
	{
		vert[0].X = (float)512/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)42/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)664/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)42/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)512/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)75/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)664/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)75/ParentWindow->GetTexture()->GetHeight();
	}

	Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
	delete[] vert;

}

bool XPushButton::IsDown()
{
	return Down;
}


void XPushButton::Update(XInputModule* input)
{
	if(Enabled)
	{
		XPosition pos = input->GetMouse().GetPosition();
		if(IsMouseOver(pos) == true && input->GetMouse().GetLeft().GetCurrent() == true && input->GetMouse().GetLeft().GetPrevious() == false)
		{
			Down = true;
			_loadTextureVertices();
		}
		else if(IsMouseOver(pos) == false || input->GetMouse().GetLeft().GetCurrent() == false)
		{
			Down = false;
			_loadTextureVertices();
		}

	}

	Render->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	Text->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	
}

XRadioButton::XRadioButton(XHash hash, XWindow* window)
	: XWidget(hash, window)
{
	Checked = false;
	Render = new XRenderPack(Hash.String + "-Layout");
	Text = new XTextPack(Hash);
	Text->SetFont(ParentWindow->GetFont());

	_loadVertices();
	_loadTextureVertices();
	window->RegisterControlsRender(Render);
	window->RegisterControlsText(Text);
	SetGeometry(XRectangle(40, 40, 25, 25));

}

void XRadioButton::_loadTextureVertices()
{

	XVertex* vert = new XVertex[4];
	if(Checked == false)
	{
		vert[0].X = (float)515/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)540/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)515/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)540/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
	}
	else
	{
		vert[0].X = (float)543/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)568/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)543/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)568/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
	}

	Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
	delete[] vert;

}

void XRadioButton::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	_loadVertices();

	Text->SetTextBox(XRectangle(rect.X + rect.W + 3, rect.Y + 2, 600, 200));
}

void XRadioButton::SetChecked()
{
	Checked = true;
	_loadTextureVertices();
}
void XRadioButton::SetUnchecked()
{
	Checked = false;
	_loadTextureVertices();
}

bool XRadioButton::IsChecked()
{
	return Checked;
}

void XRadioButton::Update(XInputModule* input)
{
	if(Enabled)
	{
		XPosition pos = input->GetMouse().GetPosition();
		if(IsMouseOver(pos) == true && input->GetMouse().GetLeft().GetCurrent() == true && input->GetMouse().GetLeft().GetPrevious() == false)
		{
			if(Checked == true)
				Checked = false;
			else
				Checked = true;

			_loadTextureVertices();
		}

	}

	Render->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	Text->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	
}

XCheckBox::XCheckBox(XHash hash, XWindow* window)
	: XWidget(hash, window)
{
	Checked = false;

	Render = new XRenderPack(Hash.String + "-Layout");
	Text = new XTextPack(Hash);
	Text->SetFont(ParentWindow->GetFont());

	_loadVertices();
	_loadTextureVertices();

	window->RegisterControlsRender(Render);
	window->RegisterControlsText(Text);

	SetGeometry(XRectangle(40, 40, 25, 25));

}

void XCheckBox::_loadTextureVertices()
{

	XVertex* vert = new XVertex[4];
	if(Checked == false)
	{
		vert[0].X = (float)570/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)594/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)570/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)594/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
	}
	else
	{
		vert[0].X = (float)596/ParentWindow->GetTexture()->GetWidth();
		vert[0].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[1].X = (float)620/ParentWindow->GetTexture()->GetWidth();
		vert[1].Y = (float)85/ParentWindow->GetTexture()->GetHeight();
		vert[2].X = (float)596/ParentWindow->GetTexture()->GetWidth();
		vert[2].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
		vert[3].X = (float)620/ParentWindow->GetTexture()->GetWidth();
		vert[3].Y = (float)110/ParentWindow->GetTexture()->GetHeight();
	}

	Render->SetTexture(ParentWindow->GetTexture(), vert, 4);
	delete[] vert;


}


void XCheckBox::SetGeometry(XRectangle rect)
{
	Rectangle = rect;
	_loadVertices();

	Text->SetTextBox(XRectangle(rect.X + rect.W + 3, rect.Y + 2, 600, 200));

}

void XCheckBox::SetChecked()
{
	Checked = true;
	_loadTextureVertices();
}
void XCheckBox::SetUnchecked()
{
	Checked = false;
	_loadTextureVertices();
}

bool XCheckBox::IsChecked()
{
	return Checked;
}

void XCheckBox::Update(XInputModule* input)
{
	if(Enabled)
	{
		XPosition pos = input->GetMouse().GetPosition();
		if(IsMouseOver(pos) == true && input->GetMouse().GetLeft().GetCurrent() == true && input->GetMouse().GetLeft().GetPrevious() == false)
		{
			if(Checked == true)
				Checked = false;
			else
				Checked = true;

			_loadTextureVertices();
		}
	}

	Render->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
	Text->SetTranslation(XVector(ParentWindow->GetGeometry()->X, ParentWindow->GetGeometry()->Y));
}